<?php

namespace common\models\help;

use Yii;
use common\helpers\Url;

/**
 * This is the model class for table "{{%help_category}}".
 *
 * @property int $id
 * @property string $name 分类名称
 * @property string $description 分类描述
 * @property string $icon
 * @property string $color
 * @property int $content_id 直接跳转ID
 * @property int $listorder 显示排序
 * @property int $status 状态
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 */
class HelpCategory extends \common\db\ActiveRecord {

    /**
     * @var integer 分类状态
     */
    const STATUS_ACTIVE = 10;
    const STATUS_DEFAULT = 0;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%help_category}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['name'], 'required'],
            [['content_id', 'listorder', 'status', 'created_at', 'updated_at'], 'integer'],
            [['name', 'icon'], 'string', 'max' => 50],
            [['description'], 'string', 'max' => 255],
            [['color'], 'string', 'max' => 7],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'name' => '分类名称',
            'description' => '分类描述',
            'icon' => 'Icon',
            'color' => 'Color',
            'content_id' => '直接跳转ID',
            'listorder' => '显示排序',
            'status' => '状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
        ];
    }

    public function getHelps() {
        return $this->hasMany(Help::className(), ['category_id' => 'id']);
    }

    public function getLink() {
        return Url::weblink('help/cate/{id}', [
            'id' => $this->id
        ]);
    }

    public function getRecommends() {
        return $this->getHelps()->orderBy([
            'listorder' => SORT_DESC,
            'views' => SORT_DESC,
        ])->limit(4);
    }

    /**
     * {@inheritdoc}
     */
    public function fields() {
        return [
            'id',
            'name',
            'icon',
            'color',
            'link',
            'recommends',
        ];
    }

}
